package com.osdp.home.service.impl;

import com.google.common.collect.Maps;
import com.osdp.home.dto.LoginUserVo;
import com.osdp.common.constant.CommonConstant;
import com.osdp.home.dto.MenuVo;
import com.osdp.home.service.IHomePageService;
import com.osdp.sc.dto.PermissionDTO;
import com.osdp.sc.dto.RoleDTO;
import com.osdp.sc.service.IScPermissionService;
import com.osdp.sc.service.IScRoleService;
import com.osdp.uc.dto.UserDTO;
import com.osdp.uc.service.IUcOrganizationService;
import com.osdp.uc.service.IUcPositionService;
import com.osdp.uc.service.IUcUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class HomePageServiceImpl implements IHomePageService {

    @Autowired
    private IUcUserService ucUserService;

    @Autowired
    private IScRoleService roleService;

    @Autowired
    private IScPermissionService permissionService;


    @Autowired
    private IUcOrganizationService organizationService;

    @Autowired
    private IUcPositionService positionService;

    @Override
    public List<PermissionDTO> queryPermissionsByUserId(String userId){
        return permissionService.queryPermissionsByUserId(userId);
    }

    @Override
    public UserDTO queryUserByUserName(String username){
        return ucUserService.queryUserByUserName(username);
    }

    @Override
    @Transactional
    public boolean updatePassWord(String id, String passWord) {
        return ucUserService.updatePassWord(id, passWord);
    }
    @Override
    public LoginUserVo  queryUserDetails(String userId){
        LoginUserVo loginUserVo = new LoginUserVo();
        UserDTO userDTO = ucUserService.queryUserById(userId);
        loginUserVo.setUser(userDTO);
        List<RoleDTO> roleVos = roleService.getRolesByUserId(userId);
        loginUserVo.setRoles(roleVos);
//        List<OrganizationVo> organizationVos = userService.quertOrganizationByUserId(userId);
//        loginUserVo.setOrganizations(organizationVos);
//        List<PositionVo> positionVos = userService.queryPositionByUserId(userId);
//        loginUserVo.setPositions(positionVos);
        return loginUserVo;
    }

    @Override
    public List<PermissionDTO> getPermissionButton(List<PermissionDTO> permissionList){
        List<PermissionDTO> list = new ArrayList<>();
        if (permissionList != null) {
            for (PermissionDTO scPermission : permissionList) {
                if (scPermission.getPermsType() == CommonConstant.FLAG_TWO_2) { // 按钮
                    list.add(scPermission);
                }
            }
        }
        return list;
    }

    /**
     * permsType为0和1的是菜单资源
     * @param permissionList
     * @return Set<MenuVo>
     */
    @Override
    public Set<MenuVo> getPermissionMenuTree(List<PermissionDTO> permissionList){
        Map<String, MenuVo> permMap = Maps.newHashMap();
        Set<MenuVo> rootPermSet = new TreeSet<>();
        if (permissionList != null) {
            for (PermissionDTO scPermission : permissionList) {
                if(scPermission.getPermsType().equals(CommonConstant.FLAG_ZERO_0)
                        || scPermission.getPermsType().equals(CommonConstant.FLAG_ONE_1)) {
                    permMap.put(scPermission.getId(), copyObject(scPermission));
                }
            }
        }
        Set<String> keys = permMap.keySet();
        for (String key : keys) {
            MenuVo vo = permMap.get(key);
            if (StringUtils.equals(vo.getParentId(),CommonConstant.TREE_ROOT_VALUE)) {
                rootPermSet.add(vo);
            } else {
                MenuVo parent = permMap.get(vo.getParentId());
                if (parent != null) {
                    parent.getChildren().add(vo);
                }
            }
        }
        return rootPermSet;
    }

    private MenuVo copyObject(PermissionDTO permissionVo){
        MenuVo menuVo = new MenuVo();
        menuVo.setId(permissionVo.getId());
        menuVo.setParentId(permissionVo.getParentId());
        menuVo.setName(permissionVo.getPermsName());
        menuVo.setPath(permissionVo.getUrl());
        menuVo.setComponent(permissionVo.getComponent());
        menuVo.setIcon(permissionVo.getIcon());
        menuVo.setSortNo(permissionVo.getSortNo());
        return menuVo;
    }
}
